<?php
class Project extends AppModel {
	var $name = 'Project';
	var $validate = array(
		'creator_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'desc' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Creator' => array(
			'className' => 'User',
			'foreignKey' => 'creator_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Task' => array(
			'className' => 'Task',
			'foreignKey' => 'project_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);


	var $hasAndBelongsToMany = array(
		'User' => array(
			'className' => 'User',
			'joinTable' => 'projects_users',
			'foreignKey' => 'project_id',
			'associationForeignKey' => 'user_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
	
	function get_user_projects($user)
	{
		if ($user['User']['role'] == 'admin')
		{
			$this->recursive = -1;
			return $this->find('all');
		}
		else
		{
			$user = $this->User->find('first', array(
				'conditions' => array('User.id' => $user['User']['id'])
			));
			
			return $user['Project'];
		}
		
	}
	
	function get_user_projects_list($user)
	{
		if ($user['User']['role'] == 'admin')
		{
			return $this->find('list', array('fields' => array('id', 'name')));
		}
		else
		{
			$user = $this->User->find('first', array(
				'conditions' => array('User.id' => $user['User']['id'])
			));
			
			if (empty($user))
			{
				return array();
			}
			
			$projects = array();
			
			foreach($user['Project'] as $project)
			{
				$projects[$project['id']] = $project['name'];
			}
			
			return $projects;
		}
	}
	
	function get_project_users($project_id)
	{
		if (empty($project_id))
		{
			return array();
		}
		$this->recursive = 2;
		$project = $this->find('first', array(
			'conditions' => array('Project.id' => $project_id)
		));
		$this->recursive = 0;
		
		if (empty($project))
		{
			return array();
		}

		$users = array();
		
		foreach($project['User'] as $user)
		{
			if ($user['role'] != 'spectator') {
				$users[] = $user['id'];
			}
		}
		
		if (empty($users))
		{
			$conditions = array('User.role' => 'admin');
		}
		else
		{
			$conditions = array(
				'OR' => array(
					'User.id IN ('.  implode(',', $users).')',
					'User.role = "admin"'
				)
			);
		}
		
		$users = $this->User->find('list', array(
			'fields' => array('id', 'username'),
			'conditions' => $conditions
		));

		return $users;
	}
	
	function get_users_projects($user)
	{
		if (empty($user))
		{
			return array();
		}
		
		$projects_list = $this->get_user_projects_list($user);
		$users_list = array();
		
		if (!empty($projects_list))
		{
			$users = $this->query('SELECT DISTINCT User.id, User.username 
				FROM projects_users, users AS User 
				WHERE User.id = user_id AND User.role <> "spectator" AND ( User.role = "admin" OR project_id IN (' . implode(',', array_keys($projects_list)) . '))');
			
			foreach ($users as $user)
			{
				$users_list[$user['User']['id']] = $user['User']['username'];
			}
		}
		
		return array($users_list, $projects_list);
	}

}
